import { defineStore } from 'pinia'
import {
  postUsersListData,
  deleteUsersListData,
  newUserData,
  editUsersData,
  postPageListData,
  deletePageByIdListData,
  addPageListData,
  editPageListData
} from '@/service/main/system/system'
import type { ISystemState } from './type'
const useSystemStore = defineStore('system', {
  // 状态
  state: (): ISystemState => ({
    usersList: [],
    usersTotalCount: 0,
    pageList: [],
    pageTotalCount: 0
  }),
  // 同步和异步请求
  actions: {
    async postUsersListAction(queryInfo: any) {
      const { data: res } = await postUsersListData(queryInfo)
      this.usersList = res.list
      this.usersTotalCount = res.totalCount
    },
    // 删除
    async deleteUsersListAction(id: number) {
      await deleteUsersListData(id)
      this.postUsersListAction({ offset: 0, size: 10 })
    },
    // 新增人员
    async newUserDataAction(formdata: any) {
      await newUserData(formdata)
      this.postUsersListAction({ offset: 0, size: 10 })
    },
    // 编辑人员
    async editUserDataAction(id: any, formdata: any) {
      await editUsersData(id, formdata)
      this.postUsersListAction({ offset: 0, size: 10 })
    },

    // 针对页面的增删改查
    async postPageListAction(pageName: string, formdata: any) {
      const { data: res } = await postPageListData(pageName, formdata)
      this.pageList = res.list
      this.pageTotalCount = res.totalCount
    },
    // 删除
    async deletePageListAction(pageName: string, id: number) {
      await deletePageByIdListData(pageName, id)
      this.postPageListAction('department', { offset: 0, size: 10 })
    },
    // 新增
    async addPageListAction(pageName: string, data: any) {
      await addPageListData(pageName, data)
      this.postPageListAction(pageName, { offset: 0, size: 10 })
    },
    // 编辑
    async editPageListAction(pageName: string, id: any, data: any) {
      console.log(pageName, id)

      await editPageListData(pageName, id, data)
      this.postPageListAction(pageName, { offset: 0, size: 10 })
    }
  }
})
export default useSystemStore
